package com.easy.controller;

import com.easy.common.api.CommonResult;
import com.easy.common.exception.Asserts;
import com.easy.domain.NoteVO;
import com.easy.service.NoteService;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author 风亦未止
 * @date 2021/9/22 16:25
 */
@Api(tags = "小纸条相关接口")
@RestController
@RequestMapping("note")
public class NoteController {

    @Autowired
    private NoteService noteService;

    @ApiOperation(value = "获取所有小纸条")
    @GetMapping("/listAll/{type}")
    public CommonResult<List<NoteVO>> list(@PathVariable("type") @ApiParam(value = "小纸条类型") int type){
//        return noteService.getAll(type);
        //改用多表联查
        return noteService.getAllList(type);
    }

    @ApiOperation(value = "获取我发布的所有小纸条")
    @PostMapping("/myList/{type}")
    public CommonResult<List<NoteVO>> myList(@PathVariable("type") @ApiParam(value = "小纸条类型") int type){
//        return noteService.getMyAll(type);
        //改用多表联查
        return noteService.getMyAllList(type);
    }

    @ApiOperation(value = "发布小纸条")
    @PostMapping("/release")
    public CommonResult release(@RequestParam("desc") @ApiParam(value = "小字条内容") String desc,
                                @RequestParam("imgUrls") @ApiParam(value = "图片url,多张图片的链接用逗号隔开") String[] imgUrls,
                                @RequestParam("type") @ApiParam(value = "发布类型") Integer type){
        System.out.println(type);
        System.out.println(Arrays.toString(imgUrls));
        if (StringUtil.isNullOrEmpty(desc.trim())){
            Asserts.fail("内容不能为空");
        }
        CommonResult release = noteService.release(desc, imgUrls,type);
        return release;
    }

    @ApiOperation(value = "小纸条评论")
    @PostMapping("/comment/{id}")
    public CommonResult comment(@PathVariable("id") @ApiParam(value = "小纸条的id") Long id,
                                @RequestParam("comment") @ApiParam(value = "评论的内容") String comment){
        if (StringUtil.isNullOrEmpty(comment.trim())){
            Asserts.fail("评论内容不能为空");
        }
        return  noteService.comment(id,comment);
    }

    @ApiOperation(value = "小纸条点赞")
    @PostMapping("/like/{id}")
    public CommonResult like(@PathVariable("id") @ApiParam(value = "小纸条的id") Long id){
       return noteService.like(id);
    }

    @ApiOperation(value = "删除纸条")
    @PostMapping("/delete/{noteId}")
    public CommonResult delete(@PathVariable("noteId") @ApiParam(value = "小纸条id") Integer noteId){
        return noteService.remove(noteId);
    }

}
